<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use zgldh\QiniuStorage\QiniuStorage;
use App\HairColor;
use App\Hair;
use App\Color;
use DB;
use Input;

class HairColorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取列表
        // $datas = HairColor::select('colorid','isdefault','hairurl','colorstatus')
        //     ->orderby('hairid')
        //     ->paginate('30');
        // return view('admin/haircolor/index',compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取color
        // $colors = Color::select('colortypeid','colorname','samplethumb')->get();
        // //获取缩略图
        // $hairs = Hair::select('hairid','hairname','hairthumb')->get();
        // //返回视图层
        // return view('admin.haircolor.add',compact('colors','hairs'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //进行验证
        $this -> validate($request,[
            'colortypeid'=>'required',
        ],[
            'colortypeid.required' => '请选择颜色',
        ]);
        //获取各个信息
        $colorstatus   =      Input::get('colorstatus');
        $isdefault     =      Input::get('isdefault');
        $hairid        =      Input::get('hairid');
        $colortypeid   =      Input::get('colortypeid');
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $extension = $file->getClientOriginalExtension();
            $extension = strtolower($extension);
            if(!in_array( $extension,['png','jpg']))
            return back() -> with(['info'=>'需要png类型或者jpg类型']);
            //首先获取它的扩展名
            $clientName = $file->getClientOriginalName();
            //将文件进行移动
            $newName = 'hair/'.md5(date('Y-m-d H:i:s') . $clientName) . "." . $extension;
            // 初始化
            $disk = QiniuStorage::disk('qiniu');

            // 上传到七牛
            $bool = $disk->put($newName, file_get_contents($file->getRealPath()));
            if($bool){
                //将数据插入数据库
                $res = HairColor::create(['colortypeid'=>$colortypeid,'hairid'=>$hairid,'isdefault'=>$isdefault,'hairurl'=>$newName,'colorstatus'=>$colorstatus]);
                if($res)
                return back() -> with(['info'=>'数据添加成功']);
                return back() -> with(['info'=>'数据添加失败']);


            }else{
                return back() -> with(['info'=>'存入七牛失败;']);

            }
        }
        return back() -> with(['info'=>'no pic']);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //根据id获得信息
        $datas = DB::table('hair_color')
            ->Leftjoin('hair_colortype','hair_color.colortypeid','=','hair_colortype.colortypeid')
            ->Leftjoin('hair_style','hair_style.hairid','=','hair_color.hairid')
            ->where('hair_color.colorid','=',$id)
            ->first();
        //返回信息
        return view('admin.haircolor.show',compact('datas'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //进行三表联查
        $datas = DB::table('hair_color')
            ->select('hair_color.colorid','hair_color.colortypeid','hair_color.hairurl','hair_color.isdefault','hair_style.hairthumb','hair_color.colorstatus')
            ->join('hair_colortype','hair_color.colortypeid','=','hair_colortype.colortypeid')
            ->join('hair_style','hair_color.hairid','=','hair_style.hairid')
            ->where('hair_color.colorid','=',$id)
            ->first();
        $colordatas = Color::select('colortypeid','colorname','samplethumb')->get();
        return view('admin.haircolor.edit',compact('datas','colordatas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //获取url
        $hairurl      = Input::get('hairurl');
        $colortypeid = Input::get('colortypeid');
        $isdefault   = Input::get('isdefault');
        $colorstatus = Input::get('colorstatus');
        if(!($request -> hasFile('file'))){
            //则没有图片传来、执行更新
            $res = HairColor::where('colorid',$id)->update(['colortypeid'=>$colortypeid,'isdefault'=>$isdefault,'colorstatus'=>$colorstatus]);
            if($res)
            return back() -> with(['info'=>'更新成功']);
            return back() -> with(['info'=>'更新失败']);
        }else {
            //则说明有图传来
            $file = Input::file('file');
            $extension = $file->getClientOriginalExtension();
            $extension = strtolower($extension);
            if (!in_array($extension, ['png', 'jpg']))
                return back()->with(['info' => '需要png类型或者jpg类型']);
            //首先或其他的扩展名
            $clientName = $file->getClientOriginalName();
            $disk = QiniuStorage::disk('qiniu');
            //先删除七牛图片，然后再存入图片
            $newhairurl = 'hair/' . md5(date('Y-m-d H:i:s') . $clientName) . "." . $extension;
            $bool = $disk->put($newhairurl, file_get_contents($file->getRealPath()));
            if ($bool) {
                //将数据插入数据库
                $res = HairColor::where('colorid', $id)->update(['colortypeid' => $colortypeid, 'isdefault' => $isdefault, 'colorstatus' => $colorstatus, 'hairurl' => $newhairurl]);
                if ($res){
                    //新的图片已经存入数据库，则删除老的图片
                    $delete = $disk->delete($hairurl);
                    if($delete)
                    return back() -> with(['info' => '更新成功']);
                    return back() -> with(['info' => '更新成功，原图片删除失败,地址是:'.$hairurl]);
                }else{
                    return back()->with(['info' => '数据库更新失败，图片已经传到七牛云,图片地址:是'.$newhairurl]);
                }
            } else {
                return back()->with(['info' => '没有图片传到七牛，请重新上传']);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //直接删除
        //删除七牛存储的数据
        $datas = HairColor::wehre('colorid',$id)->first();
        $hairurl = $datas['hairurl'];
        $disk = QiniuStorage::disk('qiniu');
        $res = HairColor::where('colorid',$id)->delete();
        if($res){
            $bool = $disk->delete($hairurl);
            if($bool){
                return Response()->json(['status'=>'success','msg'=>'删除成功']);
            }else{
                return Response()->json(['status'=>'error','msg'=>'删除七牛图片失败']);
            }
        }else{
            return Response()->json(['status'=>'error','msg'=>'删除七牛图片失败']);
        }
    }
    public function haircolorshow($hairid)
    {
            //选择
            $datas = HairColor::where('hairid',$hairid)->select('colorid','isdefault','hairurl','colorstatus')
            ->orderby('hairid')
            ->paginate('30');
            return view('admin/haircolor/index',compact('datas','hairid'));
    }
    public function addhaircolor($hairid)
    {
        //获取color
        $colors = Color::select('colortypeid','colorname','samplethumb')->get();
        $hairthumb = Hair::where('hairid',$hairid)->value('hairthumb');
        return view('admin.haircolor.add',compact('colors','hairid','hairthumb'));
    }
}
